limsOutsourceApply.detailPage = {

    getBtnToolbar: function (id, step) {
        var _this = this
        var buttons = [{
            type: 'button',
            text: 'GIKAM.BUTTON.BACK',
            icon: 'back',
            onClick: function () {
                workspace.window.goBack()
            }
        },]
        if (step) {
            if (step === 'edit') {
                buttons.push({
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                    icon: 'submit', color: 'success',
                    onClick: function () {
                        _this.submitSingle(id)
                    }
                })
            } else if (step === 'audit' || step === 'result') {
                buttons.push({
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                    icon: 'pass',
                    onClick: function () {
                        _this.passSingle(id)
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    onClick: function () {
                        _this.rejectSingle(id)
                    }
                })
            } /*else if (step === 'result') {
                buttons.push({
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                    icon: 'submit',
                    onClick: function () {
                        _this.resultSubmitSingle(id)
                    }
                })
            }*/
        }
        return {
            type: 'btnToolbar',
            items: buttons
        }
    },

    submitSingle: function (id) {
        Gikam.postText(limsOutsourceApply.baseUrl + "/submit-check", Gikam.getJsonWrapper(null, [null, [{id: id}]])).done(req => {
            req = JSON.parse(req)
            if (req.flag && req.flag === '1') {
                Gikam.create('workflow').submit({
                    data: [{id: id}],
                    pageObject: limsOutsourceApply,
                    formatterToolbar: (toolbar, type, formId) => {
                        toolbar.forEach(b => {
                            if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                b.class = 'blue'
                                b.icon = 'save'
                                b.color = 'white'
                            } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                b.icon = 'back'
                            }
                        })
                        return toolbar
                    }
                }).done(function () {
                    workspace.window.goBack()
                })
            } else {
                Gikam.alert(req.message)
            }
        }).always(function () {
        })
    },

    passSingle: function (id) {
        Gikam.create('workflow').pass({
            data: [{id: id}],
            pageObject: limsOutsourceApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    rejectSingle: function (id) {
        Gikam.create('workflow').reject({
            data: [{id: id}],
            pageObject: limsOutsourceApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    resultSubmitSingle: function (id) {
        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.WORKFLOW.BUTTON.SUBMIT', function () {
            workspace.window.showMask()
            Gikam.postText(limsOutsourceApply.baseUrl + "/result-done", Gikam.getJsonWrapper(null, [null, [{id: id}]])).done(req => {
                workspace.window.goBack()
            }).always(function () {
                workspace.window.closeMask()
            })
        })
    },

    getSampleGrid: function (id, step) {
        let _this = this
        var showResult = false
        var resultEdit = false
        if (step) {
            if (step === 'result') {
                showResult = true
                resultEdit = true
            } else if (step === 'search') {
                showResult = true
                resultEdit = false
            }
        }
        return {
            type: 'grid',
            id: 'lims-outsource-sample-edit-list-grid',
            url: limsOutsourceApply.applySampleUrl + '/queries/raw',
            service: 'limsOutsourceSampleServiceImpl',
            dbTable: 'T_LIMS_OUTSOURCE_SAMPLE',
            columns: limsOutsourceApply.getSampleEditListColumns(showResult, resultEdit),
            deleteFormatter: function (row) {
                return Gikam.propI18N('T_LIMS_OUTSOURCE_SAMPLE.SAMPLECODE') + ': ' + row.sampleCode
            },
            requestData: {
                applyId_EQ: id
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                hidden: !step || step !== 'edit',
                onClick: function () {
                    Gikam.create('modal', {
                        width: '80%',
                        height: '80%',
                        title: Gikam.choose.getConfig('batch-Sample-Product').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig('batch-Sample-Product').url + Gikam.param({
                            outsourcerId: _this.param.outsourcerId,
                            status_IN: 'inExperiment',
                            target_filter: encodeURI(JSON.stringify({
                                type: 'unassigned',
                                targetTable: 'T_LIMS_OUTSOURCE_SAMPLE',
                                targetMatchColumn: 'BATCHSAMPLEPRODUCTID',
                                thisMatchColumn: 'ID',
                                filter: [{
                                    targetFilterColumn: 'APPLYID',
                                    targetFilterValue: id
                                }]
                            }))
                        }),
                        onAfterClose: function (rows) {
                            let url = limsOutsourceApply.applySampleUrl + '/insert-multi'
                            if (!Gikam.isEmpty(rows)) {
                                var chooseData = rows.map(r => {
                                    return {
                                        sampleCode: r.sampleCode,
                                        batchCode: r.batchCode,
                                        sampleId: r.sampleId,
                                        batchId: r.batchId,
                                        batchSampleId: r.batchSampleId,
                                        // 避免后台报错，这里搞个假字段装ext里面
                                        productId: r.productId,
                                        productName: r.productName,
                                        productCode: r.productCode,
                                        applyId: id,
                                        batchSampleProductId: r.id
                                    }
                                });
                                Gikam.postText(url, Gikam.getJsonWrapper(null, [null, chooseData])).done(function () {
                                    Gikam.getComp('lims-outsource-sample-edit-list-grid').refresh()
                                });
                            }
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                hidden: !step || step !== 'edit',
                onClick: function () {
                    Gikam.getComp('lims-outsource-sample-edit-list-grid').deleteRows(limsOutsourceApply.applySampleUrl)
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.RESULT.EDIT',
                icon: 'edit',
                hidden: !step || step !== 'result',
                onClick: function () {
                    _this.setResult()
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.EXPORT',
                icon: 'export',
                hidden: !step || step !== 'result',
                onClick: function () {
                    Gikam.getText(IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                        templateCode: 'LimsOutsourceResultExportTemplate',
                        service: 'LimsOutsourceResultExportTemplate',
                        id: id
                    })).done(function (url) {
                        Gikam.download(url)
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.IMPORT',
                icon: 'import',
                hidden: !step || step !== 'result',
                onClick: function () {
                    Gikam.create('simpleUploader', {
                        id: 'outsource-sample-result-info-uploader',
                        dbTable: 'T_LIMS_OUTSOURCE_SAMPLE_RESULT',
                        bizId: id,
                        autoClose: true,
                        multiple: false,
                        accept: ['xls'],
                        onAfterClose: function (fields) {
                            if (Gikam.isNotEmpty(fields)) {
                                workspace.window.showMask()
                                let url = Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}/action/import/' + 'LimsOutsourceResultImportFileFunction', {
                                    id: fields[0].id
                                })
                                Gikam.post(url + Gikam.param({applyId: id})).done(req => {
                                    Gikam.getComp('lims-outsource-sample-edit-list-grid').refresh()
                                    if (req && req.message) {
                                        Gikam.alert(req.message)
                                    }
                                    if (req.url){
                                        Gikam.download(req.url);
                                    }
                                }).always(function () {
                                    workspace.window.closeMask()
                                })
                            }
                        }
                    })
                }
            }]
        }
    },

    setResult: function () {
        var grid = Gikam.getComp('lims-outsource-sample-edit-list-grid')
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        Gikam.createLimsModal({
            title: 'GIKAM.BUTTON.RESULT.EDIT',
            height: 200,
            width: 400,
            onConfirm: function () {
                var form = Gikam.getComp('result-edit-form')
                if (form.validate()) {
                    var data = form.getData()
                    if (Gikam.isEmpty(data)) {
                        return
                    }
                    let url = limsOutsourceApply.applySampleUrl + '/set-result'
                    workspace.window.showMask()
                    Gikam.postText(url, Gikam.getJsonWrapper(data, [null, selections])).done(req => {
                        grid.refresh()
                        Gikam.getLastModal().close(data)
                    }).always(function () {
                        workspace.window.closeMask()
                    })
                }
            },
            m: {
                type: 'form',
                id: 'result-edit-form',
                columns: 1,
                titleWidth: 80,
                disableNotEmptyValidate: true,
                fields: [{
                    field: 'testResult',
                    title: 'T_LIMS_OUTSOURCE_SAMPLE.TESTRESULT',
                    // validators: ['notEmpty'],
                },],
            }
        })
    },

    getMainTabs: function (id, step) {
        let _this = this
        var panels = [{
            title: 'T_LIMS_OUTSOURCE_SAMPLE.SERVICE_NAME',
            items: [_this.getSampleGrid(id, step)]
        },]
        if (step) {
            if (step === 'result' || step === 'search') {
                panels.push({
                    title: 'BASEMODULE.BUSINESS.OUTSOURCES.TITLE.RESULT_FILES',
                    items: [{
                        type: 'uploader',
                        id: 'lims-outsource-apply-result-uploader',
                        dbTable: 'T_LIMS_OUTSOURCE_APPLY_RESULT',
                        readonly: step !== 'result',
                        bizId: id,
                    }]
                })
            }
        }
        panels.push({
            title: 'GIKAM.FILE.ATTACHMENT',
            items: [{
                type: 'uploader',
                id: 'lims-outsource-apply-detail-uploader',
                dbTable: 'T_LIMS_OUTSOURCE_APPLY',
                readonly: step !== 'edit',
                bizId: id,
            }]
        }, {
            title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
            items: [{
                type: 'grid',
                id: 'lims-outsource-apply-audit-record',
                columns: limsOutsourceApply.getFlowListColumns(),
                url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_OUTSOURCE_APPLY&id=' + _this.param.id
            }]
        },)
        return {
            type: 'tab',
            panels: panels
        }
    },

    createPage: function () {
        let _this = this
        Gikam.create('layout', {
            center: {
                items: [this.getBtnToolbar(_this.param.id, _this.param.step), this.getMainTabs(_this.param.id, _this.param.step)]
            },
            renderTo: workspace.window.$dom
        })
        return this
    },

    init: function (param) {
        this.param = param
        this.createPage()
    }
}
